package com.happyass.show.service.impl;

import com.happyass.show.dao.ActivityDao;
import com.happyass.show.dao.AddrDao;
import com.happyass.show.entity.Activity;
import com.happyass.show.entity.Addr;
import com.happyass.show.service.ActivityService;
import net.minidev.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * TODO
 *
 * @author admin
 * @date 2021/1/5 9:51
 *
 * <P></P>
 **/

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private AddrDao addrDao;


    @Override
    public List<Activity> findAll() {
        return activityDao.findAll();
    }

    //后台表演节目表显示 分页
    @Override
    public HashMap<String, Object> activityList(Activity activity) {

        //设置分页的参数
        Pageable pageable = PageRequest.of(activity.getPage()-1,activity.getRow());
        Page<Activity> activityPage = activityDao.findByActivityDel(1,pageable);

        HashMap<String,Object> map = new HashMap<>();
        //当前页
        map.put("activityList",activityPage.getContent());
        //数据总条数
        map.put("total",activityPage.getTotalElements());
        //页数
        map.put("totalPage",activityPage.getTotalPages());
        System.out.println("页数"+activityPage.getTotalPages());
        //上一页
        if(activityPage.getNumber() <=0 ){
            map.put("prePage",activityPage.getNumber()+1);
        }else{
            map.put("prePage",activityPage.getNumber());
        }
        //下一页
        if((activityPage.getNumber()+2) >= activityPage.getTotalPages()){
            map.put("nextPage",activityPage.getTotalPages());
        }else{
            map.put("nextPage",activityPage.getNumber()+2);
        }
        //当前页
        map.put("curPage",activityPage.getNumber()+1);
        return map;

    }

    //停用启用状态修改
    @Override
    public HashMap<String, Object> acivityStatus(Integer activityId, Integer activityStatus) {
        HashMap<String,Object> map = new HashMap<>();
        Activity activity = activityDao.findByActivityId(activityId);
        if (activity.getActivityStatus()!=activityStatus){
            activity.setActivityStatus(activityStatus);
            activityDao.save(activity);
            map.put("info","success");
        }else {
            map.put("info","fail");
        }
        return map;
    }

    //后台表演节目新增
    @Override
    public HashMap<String, Object> activityAdd(Activity activity) {
        HashMap<String,Object> map = new HashMap<>();
        if (activityDao.findByActivityNameAndActivityDel(activity.getActivityName(),0)!=null){
            map.put("info","新增失败，该活动已存在");
        }else {
            Addr addr = addrDao.findByAddrName(activity.getActivityAddr());
            activity.setAddr(addr);
            activityDao.save(activity);
            map.put("info","新增成功");
        }
        return map;
    }

    //后台表演节目新增  下拉框实时更新
    @Override
    public Set<String> activityArea(String activityAddr) {
        return activityDao.selectAreaByAddr(activityAddr);
    }

    //后台表演节目删除
    @Override
    public HashMap<String, Object> activityDel(Integer activityId) {
        HashMap<String,Object> map = new HashMap<>();
        Activity activity = activityDao.findByActivityId(activityId);
        //伪删除 1为未删除 0为已删除
        if (activity.getActivityDel()==1){
            activity.setActivityDel(0);
            activityDao.save(activity);
            map.put("info","success");
        }else {
            map.put("info","fail");
        }
        return map;
    }

    //后台表演节目批量删除
    @Override
    public HashMap<String, Object> activitysDel(String ids) {
        HashMap<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<Integer> activityIdList = new ArrayList<>();
        for (String s : split) {
            activityIdList.add(Integer.parseInt(s));
        }
        String failActivityId = "";
        int failTime=0;
        for (Integer activityId : activityIdList) {
            if (activityDao.findByActivityId(activityId)!=null){
                Activity act = activityDao.findByActivityId(activityId);
                //伪删除 1为未删除 0为已删除
                if (act.getActivityDel()==1){
                    act.setActivityDel(0);
                    activityDao.save(act);
                }else {
                    failActivityId += activityId+" ";
                    failTime += 1;
                }
            }else {
                map.put("info","编号为 "+activityId+" 的节目不存在！");
                return map;
            }
        }
        if (failTime>0){
            map.put("info","编号为 "+failActivityId+" 的节目删除失败，请重试");
        }else {
            map.put("info","success");
        }
        return map;
    }

    //后台表演节目修改 显示原始数据
    @Override
    public Activity update(Integer activityId) {
        return activityDao.findByActivityId(activityId);
    }


    //后台表演节目所在具体地址修改  下拉框显示  不包括原始数据
    @Override
    public Set<String> findExceptActivityArea(Integer activityId) {
        Activity activity = activityDao.findByActivityId(activityId);
        return activityDao.selectAreaByAddrWithoutArea(activity.getActivityAddr(),activity.getActivityArea());
    }

    @Override
    public HashMap<String, Object> activityUpdate(Activity activity) {
        HashMap<String,Object> map = new HashMap<>();
        if (activity!=null){
            Activity act = activityDao.findByActivityId(activity.getActivityId());
            Addr addr = addrDao.findByAddrName(activity.getActivityAddr());
            act.setAddr(addr);
            act.setActivityDel(activity.getActivityDel());
            act.setActivityStatus(activity.getActivityStatus());
            act.setActivityAddr(activity.getActivityAddr());
            act.setActivityArea(activity.getActivityArea());
            act.setActivityName(activity.getActivityName());
            act.setDayTime(activity.getDayTime());
            activityDao.save(act);
            map.put("info","修改成功");
        }else {
            map.put("info","修改失败");
        }

        return map;
    }


    @Override
    public HashMap<String, Object> activitySearch(String searchKey, String searchValue, Activity activity) {
        HashMap<String, Object> map = new HashMap<>();
        //设置分页的参数
        Pageable pageable = PageRequest.of(activity.getPage() - 1, activity.getRow());
        Page<Activity> activityPage = null;
        if (searchKey.equals("activityId")) {
            boolean num = true;
            for (int i = 0; i < searchValue.length(); i++){
                if (!Character.isDigit(searchValue.charAt(i))){
                    num = false;
                    break;
                }
            }
            if (num){
                activityPage = activityDao.findByActivityIdLikeAndActivityDel(Integer.parseInt(searchValue), 1, pageable);
            }else {
                activityPage = activityDao.findByActivityIdLikeAndActivityDel(999999999, 1, pageable);
            }
        } else if (searchKey.equals("activityName")) {
            activityPage = activityDao.findByActivityNameContainingAndActivityDel(searchValue, 1, pageable);
        } else if (searchKey.equals("activityAddr")) {
            activityPage = activityDao.findByActivityAddrContainingAndActivityDel(searchValue, 1, pageable);
        } else if (searchKey.equals("activityArea")) {
            activityPage = activityDao.findByActivityAreaContainingAndActivityDel(searchValue, 1, pageable);
        } else if (searchKey.equals("dayTime")) {
            if (searchValue.equals("白天场")){
                activityPage = activityDao.findByDayTimeLikeAndActivityDel(1, 1, pageable);
            }else if (searchValue.equals("夜间场")){
                activityPage = activityDao.findByDayTimeLikeAndActivityDel(2, 1, pageable);
            }else {
                activityPage = activityDao.findByDayTimeLikeAndActivityDel(9, 1, pageable);
            }
        } else if (searchKey.equals("activityStatus")) {
            if (searchValue.equals("启用")){
                activityPage = activityDao.findByActivityStatusLikeAndActivityDel(1, 1, pageable);
            }else if (searchValue.equals("停用")){
                activityPage = activityDao.findByActivityStatusLikeAndActivityDel(0, 1, pageable);
            }else {
                activityPage = activityDao.findByActivityStatusLikeAndActivityDel(9, 1, pageable);
            }
        }else {
            activityPage = activityDao.findByActivityDel(1, pageable);
            map.put("back",1);
        }
        //当前页
        map.put("activityList", activityPage.getContent());
        //数据总条数
        map.put("total", activityPage.getTotalElements());
        //页数
        map.put("totalPage", activityPage.getTotalPages());
        System.out.println("页数" + activityPage.getTotalPages());
        //上一页
        if (activityPage.getNumber() <= 0) {
            map.put("prePage", activityPage.getNumber() + 1);
        } else {
            map.put("prePage", activityPage.getNumber());
        }
        //下一页
        if ((activityPage.getNumber() + 2) >= activityPage.getTotalPages()) {
            map.put("nextPage", activityPage.getTotalPages());
        } else {
            map.put("nextPage", activityPage.getNumber() + 2);
        }
        //当前页
        map.put("curPage", activityPage.getNumber() + 1);
        map.put("searchValue",searchValue);
        map.put("searchKey",searchKey);
        return map;
    }

    //后台演出计划新增  查询所有启用的演出节目
    @Override
    public JSONArray findActivity() {
        JSONArray jsonArray = new JSONArray();
        List<Activity> activityList = activityDao.findByActivityStatusLikeAndActivityDel(1, 1);
        for (Activity activity : activityList) {
            HashMap<String,Object> map = new HashMap<>();

            map.put("value",activity.getActivityId().toString());
            if(activity.getDayTime()==1){
                map.put("title",activity.getActivityName()+"（白天场9:00-21:00）");
            }else if (activity.getDayTime()==2){
                map.put("title",activity.getActivityName()+"（夜间场17:00-21:00）");
            }
            map.put("dayTime",activity.getDayTime());
            jsonArray.add(map);
        }
        return jsonArray;
    }

    @Override
    public HashMap<String, Object> findRetrieve(Activity activity) {
        //设置分页的参数
        Pageable pageable = PageRequest.of(activity.getPage()-1,activity.getRow());
        Page<Activity> activityPage = activityDao.findByActivityDel(0,pageable);

        HashMap<String,Object> map = new HashMap<>();
        //当前页
        map.put("activityList",activityPage.getContent());
        //数据总条数
        map.put("total",activityPage.getTotalElements());
        //页数
        map.put("totalPage",activityPage.getTotalPages());
        System.out.println("页数"+activityPage.getTotalPages());
        //上一页
        if(activityPage.getNumber() <=0 ){
            map.put("prePage",activityPage.getNumber()+1);
        }else{
            map.put("prePage",activityPage.getNumber());
        }
        //下一页
        if((activityPage.getNumber()+2) >= activityPage.getTotalPages()){
            map.put("nextPage",activityPage.getTotalPages());
        }else{
            map.put("nextPage",activityPage.getNumber()+2);
        }
        //当前页
        map.put("curPage",activityPage.getNumber()+1);
        return map;
    }

    @Override
    public HashMap<String, Object> activityRes(Integer activityId) {
        HashMap<String,Object> map = new HashMap<>();
        Activity activity = activityDao.findByActivityId(activityId);
        //伪删除 1为未删除 0为已删除
        if (activity.getActivityDel()==0){
            activity.setActivityDel(1);
            activityDao.save(activity);
            map.put("info","success");
        }else {
            map.put("info","fail");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> activitysRes(String ids) {
        HashMap<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<Integer> activityIdList = new ArrayList<>();
        for (String s : split) {
            activityIdList.add(Integer.parseInt(s));
        }
        String failActivityId = "";
        int failTimes=0;
        for (Integer activityId : activityIdList) {
            if (activityDao.findByActivityId(activityId)!=null){
                Activity act = activityDao.findByActivityId(activityId);
                //伪删除 1为未删除 0为已删除
                if (act.getActivityDel()==0){
                    act.setActivityDel(1);
                    activityDao.save(act);
                }else {
                    failActivityId += activityId+" ";
                    failTimes += 1;
                }
            }else {
                map.put("info","编号为 "+activityId+" 的节目不存在！");
                return map;
            }
        }
        if (failTimes>0){
            map.put("info","编号为 "+failActivityId+" 的节目恢复失败，请重试");
        }else {
            map.put("info","success");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> retrieveSearch(String searchKey, String searchValue, Activity activity) {
        HashMap<String, Object> map = new HashMap<>();
        //设置分页的参数
        Pageable pageable = PageRequest.of(activity.getPage() - 1, activity.getRow());
        Page<Activity> activityPage = null;
        if (searchKey.equals("activityId")) {
            boolean num = true;
            for (int i = 0; i < searchValue.length(); i++){
                if (!Character.isDigit(searchValue.charAt(i))){
                    num = false;
                    break;
                }
            }
            if (num){
                activityPage = activityDao.findByActivityIdLikeAndActivityDel(Integer.parseInt(searchValue), 0, pageable);
            }else {
                activityPage = activityDao.findByActivityIdLikeAndActivityDel(999999999, 0, pageable);
            }
        } else if (searchKey.equals("activityName")) {
            activityPage = activityDao.findByActivityNameContainingAndActivityDel(searchValue, 0, pageable);
        } else if (searchKey.equals("activityAddr")) {
            activityPage = activityDao.findByActivityAddrContainingAndActivityDel(searchValue, 0, pageable);
        } else if (searchKey.equals("activityArea")) {
            activityPage = activityDao.findByActivityAreaContainingAndActivityDel(searchValue, 0, pageable);
        } else if (searchKey.equals("dayTime")) {
            if (searchValue.equals("白天场")){
                activityPage = activityDao.findByDayTimeLikeAndActivityDel(1, 0, pageable);
            }else if (searchValue.equals("夜间场")){
                activityPage = activityDao.findByDayTimeLikeAndActivityDel(2, 0, pageable);
            }else {
                activityPage = activityDao.findByDayTimeLikeAndActivityDel(9, 0, pageable);
            }
        }else {
            activityPage = activityDao.findByActivityDel(0, pageable);
            map.put("back",1);
        }
        //当前页
        map.put("activityList", activityPage.getContent());
        //数据总条数
        map.put("total", activityPage.getTotalElements());
        //页数
        map.put("totalPage", activityPage.getTotalPages());
        System.out.println("页数" + activityPage.getTotalPages());
        //上一页
        if (activityPage.getNumber() <= 0) {
            map.put("prePage", activityPage.getNumber() + 1);
        } else {
            map.put("prePage", activityPage.getNumber());
        }
        //下一页
        if ((activityPage.getNumber() + 2) >= activityPage.getTotalPages()) {
            map.put("nextPage", activityPage.getTotalPages());
        } else {
            map.put("nextPage", activityPage.getNumber() + 2);
        }
        //当前页
        map.put("curPage", activityPage.getNumber() + 1);
        map.put("searchValue",searchValue);
        map.put("searchKey",searchKey);
        return map;
    }

}
